/**
 * 配置
 * */
package org.zmhhxl.sample3.oauth2.a.authentications.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.security.oauth2.core.oidc.IdTokenClaimNames;
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
import org.springframework.security.oauth2.core.oidc.OidcUserInfo;
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
import org.springframework.security.oauth2.core.user.OAuth2User;

import java.util.Map;

@Data
@JsonSerialize
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@JsonIgnoreProperties(ignoreUnknown = true)
public class CustomOidcUser  extends CustomOAuth2User implements OidcUser {
   /**
    * 用户账号
    */
   private String sub;

   /**
    * OIDC 请求中的idToken
    */
   private OidcIdToken idToken;

   /**
    * 用户信息
    */
   private OidcUserInfo userInfo;

   public CustomOidcUser(OAuth2User delegate, OidcIdToken idToken, OidcUserInfo userInfo,  String nameAttributeKey) {
      super(delegate, nameAttributeKey);
      this.idToken = idToken;
      this.userInfo = userInfo;
   }

   public CustomOidcUser(OAuth2User delegate,OidcIdToken idToken, OidcUserInfo userInfo) {
      this( delegate, idToken, userInfo, IdTokenClaimNames.SUB);  // ==idToken.getSubject()
   }

   /**
    * Returns the claims about the user. The claims are aggregated from
    * {@link #getIdToken()} and {@link #getUserInfo()} (if available).
    *
    * @return a {@code Map} of claims about the user
    */
   @Override public Map<String, Object> getClaims() {
      return this.getAttributes();
   }
}
